package com.tdr.myandroid.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Tony Guo on 2014/7/15.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    final private String createEventsString = "CREATE TABLE events(id INTEGER NOT NULL," +
            " content TEXT, update_date DATETIME ,PRIMARY KEY (id));";
    final private String createConditionString = "CREATE TABLE conditions(event_id INTEGER," +
            " trigger_type INTEGER, trigger_condition TEXT," +
            "FOREIGN KEY(event_id) REFERENCES events(id) );";
    final private String createActionString = "CREATE TABLE actions(event_id INTEGER," +
            " action_type INTEGER, action_target TEXT, action_content TEXT, action_extra_info TEXT" +
            "FOREIGN KEY(event_id) REFERENCES events(id) );";
    private static DatabaseHelper db = null;

    public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(createEventsString);
        db.execSQL(createConditionString);
        db.execSQL(createActionString);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public static synchronized DatabaseHelper getDbHelper() {
        if (db == null) {
            db = new DatabaseHelper(null, "AssitDB.db3", null, 1);
        }
        return db;
    }
}
